﻿#region copyright.anytao.com
// Copyright   : 2008, Anytao.com
// Author      : Anytao，http://www.anytao.com
// FileName    : Anytao.ISpace.BLL
// Release     : 2008/06/07 1.0
// Description : BLL for user
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Security.Principal;
using System.Web.Security;
using Anytao.ISpace.DAL;
using Anytao.ISpace.Entity;

namespace Anytao.ISpace.BLL
{
    public class UserBLL : IPrincipal
    {
        private int userID = int.MinValue;
        private UserLevel level;
        private FormsIdentity userIdentity;

        private static UserDAL userDal = new UserDAL();

        public int ID
        {
            get { return userID; }
        }

        public UserLevel Level
        {
            get { return level;}
        }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get { return userIdentity; }
        }

        public bool IsInRole(string role)
        {
            try
            {
                UserLevel level = (UserLevel)Enum.Parse(typeof(UserLevel), role, true);
                return level == this.level;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region public Methods

        public static UserBLL Login(string loginName, string password)
        {
            UserBLL userBll = new UserBLL();
            User user = userDal.GetUser(loginName, password);
            userBll.userID = user.UserID;
            //userBll.level = Enum.Parse(typeof(UserLevel), user.Role.RoleLevel, true);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                2, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(30),
                false, user.Role.RoleLevel + "|" + user.UserID.ToString());

            userBll.userIdentity = new FormsIdentity(ticket);

            return userBll;
        }

        #endregion

    }
}
